var host = "http://localhost:8080/";

function getChannels(param,callback) {
    formRequest("channel/list",param,"post",callback)
}

function delGoods(id) {
    formRequest("goods/"+id, {}, "delete");
}

function upStatus(id) {
    formRequest("goods/upStatus/"+id,{},"get")

}
function downStatus(id) {
    formRequest("goods/downStatus/"+id,{},"get")
}

function menus(callback) {
    formRequest("resources/menus", {}, "get", callback);
}


function getGoods(param, callback) {
    formRequest("goods/goods", param, "post", callback);
}

function login(param, callback) {
    formRequest("passport/signin", param, "post", callback);
}

function addGoods(param, callback) {
    jsonRequest("goods/", param, "post", callback);
}

/**
 * 树状产品分类
 * @param callback 数据加载回调
 */
function getLevel(callback) {
    formRequest("category/asLevel", {}, "get", callback, false);
}

function getGood(data, callback) {
    request("goods/" + data["id"], {}, "get", callback);
}

function uploadFile(data, callback) {
    multipartRequest("file/", data, "post", callback)
}
function findAllKnowledge(knowledgename,type,page,callback) {
    request("admin/wxappHelp/"+page+"/10/" +knowledgename+"/"+type, {}, "post", callback,"application/json;charset=utf-8",false);
}
function findAllKnowledgeByType(type,page,callback) {
    request("admin/wxappHelp/findAllByType/"+page+"/10/"+type, {}, "post", callback,"application/json;charset=utf-8",false);
}
function getKnowledgeImage(id,callback) {
    request("admin/wxappHelp/getImage/" + id, {}, "get", callback);
}
function getKonwledgeByCreateTimeDESC(type,page,callback) {
    request("admin/wxappHelp/"+page+"/10/"+type, {}, "get", callback);


}
function deleteKnowledge(id) {
    request("admin/wxappHelp/delete/"+id, {}, "DELETE",null);
}
function getKnowledgeById(id,callback) {
    request("admin/wxappHelp/findOneById/"+id, {}, "get",callback);

}
function updateKnowledge(data,callback) {
    request("admin/wxappHelp/update/",data,"post",callback,false,false);
}
function addKnowledge(data,callback) {
    request("admin/wxappHelp/add/",data,"post",callback,false,false);
}

function getMeetingByTitle(page,title,type,callback) {
    request("admin/trainMeeting/"+page+"/10/" +title+"/"+type, {}, "post", callback,"application/json;charset=utf-8",false);

}
function getAllMeeting(page,type,callback) {
    request("admin/trainMeeting/"+page+"/10/"+type , {}, "get", callback);

}
function deleteMeeting(id) {
    request("admin/trainMeeting/delete/"+id, {}, "DELETE",null);
}
function getMeetingByid(mid,callback) {
    request("admin/trainMeeting/findByID/"+mid, {}, "get",callback);

}
function updateMeeting(mid,data,callback) {
    request("admin/trainMeeting/update/"+mid,data,"put",callback,"application/json;charset=utf-8",false);
}
function addMetting(data,callback) {
    request("admin/trainMeeting/addMeeting/",data,"post",callback,"application/json;charset=utf-8",false);
}
/**     Train       */
function getTrianByTitle(title,callback) {
    request("admin/trainMeeting/findTrainByTitleDESC/"+title, {}, "get",callback);
}
function getAllTrain(callback) {
    request("admin/trainMeeting/findAllTrainDESC/", {}, "get",callback);

}
function getTrainById(id,callback) {
    request("admin/trainMeeting/findByID/"+id, {}, "get",callback);

}
function addTrain(data,callback) {
    request("admin/trainMeeting/addTrain/",data,"post",callback,"application/json;charset=utf-8",false);

}
/**     userExamine   */
function getExaminById(id,callback) {
    request("admin/userExamine/findOneById/"+id, {}, "get",callback);

}
function getExamineImageById(id,callback) {
    request("admin/userExamine/getImage/"+id, {}, "get",callback);


}
function agreeExamineApi(id,callback) {
    request("admin/userExamine/examSuccess/"+id,{},"put",callback,"application/json;charset=utf-8",false);

}
function getExamineByUserid(page,uid,callback) {
    request("admin/userExamine/"+page+"/10/" +uid, {}, "post", callback);

}
function getExamineList(page,type,callback) {

    request("admin/userExamine/"+page+"/10/"+type, {}, "get", callback);

}
function FaultExamine(id,re,callback) {
    request("admin/userExamine/examFault/"+id+"/"+re,{},"put",callback,"application/json;charset=utf-8",false);

}
function getSuccessExamineList(callback) {

    request("admin/userExamine/successlist/", {}, "get",callback);
}
function getFailExamineList(callback) {

    request("admin/userExamine/faillist/", {}, "get",callback);
}
/**     Order    */
function getAllOrder(page,type,callback) {
    request("admin/order/"+page+"/10/"+type, {}, "post", callback);
}
function getgoodsNum(id,callback) {
    request("admin/orderGoods/findGoodsNumByOrderId/"+id, {}, "get",callback,false,false,false);
}
function getgoods(id,callback) {
    request("admin/orderGoods/findGoodsByOrderId/"+id, {}, "get",callback,false,false,false);

}
function DeliveryByID(ecompany,eno,id,callback) {
    request("admin/order/Delivery/"+ecompany+"/"+eno+"/"+id, {}, "get", callback);
}

function getOrderByid(id,callback) {
    request("admin/order/findOrderByid/"+id, {}, "get",callback);
}
function getAdressByid(id,callback) {
    request("admin/orderAddress/findOneByOrderId/"+id, {}, "get",callback);
}
function getOrderImageById(id,callback) {
    request("admin/orderGoods/getImage/"+id, {}, "get",callback);
}
//Channel
function getRegionById(id,callback) {
    request("admin/region/getLevel/"+id, {}, "get",callback);
}
function getAllNextRegionById(id,callback) {
    request("admin/region/getAllnextRegion/"+id, {}, "get",callback);
}
function getLevel1ByRegionId(id,callback) {
    request("admin/region/getLevel1ByRegionId/"+id, {}, "get",callback);
}
function getLevel2ByRegionId(id,callback) {
    request("admin/region/getLevel2ByRegionId/"+id, {}, "get",callback);
}
function getRegionsByRegionId(rid,callback){
    request("admin/orderAddress/getOrdersByRegionId/"+rid, {}, "post",callback);
}
function getUser(callback) {
    request("resources/getuser", {}, "get",callback);
}
function getChannel(uid,callback) {
    request("admin/channel/getChannel/"+uid, {}, "get",callback);
}


function jsonRequest(url, data, method, callback) {
    request(url, JSON.stringify(data), method, callback, "application/json", false);
}

function formRequest(url, data, method, callback, async) {
    request(url, data, method, callback, "application/x-www-form-urlencoded", false, async);
}

function multipartRequest(url, data, method, callback) {
    request(url, data, method, callback, false, false);
}

function request(url, data, method, callback, contentType, processsData, async) {
    async = !!async;
    $.ajax({
        method: method,
        url: host + url,
        data: data,
        dataType: "json",
        contentType: contentType,
        processData: processsData,
        async: async,
        success: function (res) {
            console.log(res)
            if (res.status == 200) {
                if (!res.data)
                    layer.alert(res.message)
                else
                    callback(res.data);
            } else
                layer.alert(res.message)

        },
        error: function (jqXHR, textStatus, errorThrown) {
            layer.alert(errorThrown)
        }
    });
}

